import requests
import os
import re
import json
from fake_useragent import UserAgent

url = "https://www.youtube.com/playlist?list=PLqPwaZ9SK946m3pC5oDOYHcYZpgcajf9X"

base_dir = "/home/file/youtube/Scorpo"
base_list_dir = "/Jojo Balkan Adventures"

# 保存路径
list_dir = f"{base_dir}{base_list_dir}"


def get_data(url):
    headers = {
        "user-agent": UserAgent().random,
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
        "cookie": "VISITOR_INFO1_LIVE=MNXH0goHiro; VISITOR_PRIVACY_METADATA=CgJVUxICGgA%3D; PREF=f4=4000000&tz=Asia.Shanghai; HSID=AtacJxg_HQ1BUyC2_; SSID=AnRp5a1WDnj0hkhFc; APISID=yNAN55eSX9acwbtm/A5-w89EgAQ5JhgHwD; SAPISID=DL8Mo5Rbnaa_L9Wz/AtaiczNrLHyzbXr7J; __Secure-1PAPISID=DL8Mo5Rbnaa_L9Wz/AtaiczNrLHyzbXr7J; __Secure-3PAPISID=DL8Mo5Rbnaa_L9Wz/AtaiczNrLHyzbXr7J; LOGIN_INFO=AFmmF2swRQIhAJ5EgVx_ft5rJe-ftXW4cqsqrU8i8HXcNmi79iwrRdvHAiA8-XdwS3NHQ-z9xZTRFiEg0j9vADpDO200nGw_7zSuhQ:QUQ3MjNmd0pEcC1ZSnFKVk0tMzRDaDRiVU0xX0M4V3h2U2kxeHhuZm9iS3pUMVJOblFmMW92WC0waTREa01NaW5JUGN5VEhkUE5CbHVHSHhiVHFiYUlPcDdkM0FQdmQ5bEgtemIwYmpqQU9FMkVScy13enBaaDFlRmhpd3lqWEFNOEpPYVpBc1ZYckVHNkNKOGhPRGZhOGVOT21sbE85ZWln; YSC=Pfd4O9HGPeA; SID=bAg2WHS2_mjDrQRVasIvd79zeAu7zR7sTFg0f-9xLC5b4orXyz2cK3lHWH6pkDaxVHoofQ.; __Secure-1PSID=bAg2WHS2_mjDrQRVasIvd79zeAu7zR7sTFg0f-9xLC5b4orXcQhfkZ8fDNZj315EOdsJ_A.; __Secure-3PSID=bAg2WHS2_mjDrQRVasIvd79zeAu7zR7sTFg0f-9xLC5b4orXXHE91f8wJjnRQUDHFulMMA.; __Secure-1PSIDTS=sidts-CjEB3e41hXLbrYdz_Yz1HKkYTBeFR_LCjYegGBYFNs6NJaLY5Ch_0R3SF-fETr5iSGZkEAA; __Secure-3PSIDTS=sidts-CjEB3e41hXLbrYdz_Yz1HKkYTBeFR_LCjYegGBYFNs6NJaLY5Ch_0R3SF-fETr5iSGZkEAA; SIDCC=APoG2W8dYQ4LRnHwcfDYquRvriaFuV5OGEjc0Hg9sKJQ0noEeU0gu4NPv4lC6yw_gOL3qgBKkzs; __Secure-1PSIDCC=APoG2W9Ja3lvOBDAdqjPcJe6SRCXNrD2JNjFtt5wNnuvrrCPprb5x6jWT5H9-ZemI6_9jLyVcQ; __Secure-3PSIDCC=APoG2W-TWEglUFUit4A-IuG-xScg5zCOBlMWT3TjaLnZRykHeAbbNhjzyPNdxpVtUk4em8zcxw",
    }

    response = requests.get(url, headers=headers)
    resHtml = response.text

    data_json_text = re.findall(r"var ytInitialData = (.+?);", resHtml, re.M)
    if len(data_json_text) == 0:
        return

    data_dict = json.loads(str(data_json_text[0]))

    process_list_info(data_dict, resHtml)
    process_video_info(data_dict["contents"])


def process_list_info(data, html):
    f_page = open(f"{list_dir}/page.html", "w", encoding="utf-8")
    f_data = open(f"{list_dir}/data.json", "w", encoding="utf-8")
    f_info = open(f"{list_dir}/info.json", "w", encoding="utf-8")

    data_header = data["header"]["playlistHeaderRenderer"]
    info = {
        "type": "video-list",
        "playlistId": data_header["playlistId"],
        "title": data_header["title"],
        "descriptionText": data_header["descriptionText"],
        "numVideosText": data_header["numVideosText"],
        "ownerText": data_header["ownerText"],
        "viewCountText": data_header["viewCountText"],
        "privacy": data_header["privacy"],
        "serviceEndpoints": data_header["serviceEndpoints"],
        "thumbnail": data_header["playlistHeaderBanner"][
            "heroPlaylistThumbnailRenderer"
        ]["thumbnail"],
    }

    f_info.write(json.dumps(info))
    f_data.write(json.dumps(data))
    f_page.write(html)

    f_info.close()
    f_page.close()
    f_data.close()


def process_video_info(data):
    video_list = data["twoColumnBrowseResultsRenderer"]["tabs"][0]["tabRenderer"][
        "content"
    ]["sectionListRenderer"]["contents"][0]["itemSectionRenderer"]["contents"][0][
        "playlistVideoListRenderer"
    ][
        "contents"
    ]
    for video in video_list:
        video_data = video["playlistVideoRenderer"]
        video_name = video_data["title"]["runs"][0]["text"]
        video_dir = f"{list_dir}/{video_name}"

        if not os.path.exists(video_dir):
            os.makedirs(video_dir)

        f_info = open(f"{video_dir}/info.json", "w", encoding="utf-8")
        f_data = open(f"{video_dir}/data.json", "w", encoding="utf-8")

        info = {
            "videoId": video_data["videoId"],
            "url": f"https://www.youtube.com/watch?v={video_data['videoId']}",
            "title": video_name,
            "label": video_data["title"]["accessibility"]["accessibilityData"]["label"],
            "thumbnails": video_data["thumbnail"]["thumbnails"],
            "shortBylineText": video_data["shortBylineText"],
            "lengthText": video_data["lengthText"],
            "lengthSeconds": video_data["lengthSeconds"],
            "videoInfo": video_data["videoInfo"],
        }

        f_info.write(json.dumps(info))
        f_info.close()
        f_data.write(json.dumps(video_data))
        f_data.close()


if __name__ == "__main__":
    dir_exists = os.path.exists(list_dir)
    # 如果目录不存在，创建目录
    if not dir_exists:
        os.makedirs(list_dir)

    get_data(url)
